import 'package:flutter/material.dart';
import '../searchPage.dart';
import '../../components/readTabPage.dart';

class LoveRead extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new _LoveReadState();
  }
}

class _LoveReadState extends State<LoveRead> with TickerProviderStateMixin {
  List<Map<String, String>> _readTabs = <Map<String, String>>[
    {'title': '关注', 'category': 'favoriate'},
    {'title': '推荐', 'category': 'recommend'},
    {'title': '美食', 'category': 'food'},
    {'title': '穿着', 'category': 'dress'},
    {'title': '运动', 'category': 'sport'},
    {'title': '程序', 'category': 'program'},
    {'title': '美工', 'category': 'artwork'},
    {'title': '产品', 'category': 'production'},
    {'title': 'MySql', 'category': 'mysql'},
  ];

  TabController _readTabController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _readTabController = new TabController(length: _readTabs.length, vsync: this);
//    _readTabController.addListener(() {}); //这里的回调会执行两次
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _readTabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      backgroundColor: Colors.white,
      appBar: new AppBar(
        title: new Text('Read'),
        bottom: new TabBar(
          isScrollable: true,
          tabs: _readTabs.map((Map<String, String> item) {
            return new Tab(text: item['title']);
          }).toList(),
          controller: _readTabController,
        ),
        actions: <Widget>[
          new Container(
            padding: const EdgeInsets.symmetric(horizontal: 6.0),
            child: new IconButton(
              icon: new Icon(Icons.search),
              onPressed: () {
                Navigator.push(
                  context,
                  new MaterialPageRoute<void>(
                    builder: (BuildContext context) => new Search(),
                    fullscreenDialog: true,
                  ),
                );
              },
            ),
          ),
        ],
      ),
      body: new TabBarView(
        children: _readTabs.map((Map<String, String> item) {
          return new ReadTabPage(category: item['category']);
        }).toList(),
        controller: _readTabController,
      ),
    );
  }
}
